#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#define PATH "wifidata.ini"
#define Debug
#define LOG_MAX_MSG_LEN (2048)

#ifdef Debug
#define slog(format,...)   printf("[%s][%d] "format"", __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define slog(format,...)
#endif
/*
void slog(char *fmt, ...)
{
#ifdef Debug
	va_list ap;
        char stmp[LOG_MAX_MSG_LEN];

        memset(stmp, 0x00, sizeof(stmp));

        va_start(ap,fmt);
        vsnprintf(stmp,LOG_MAX_MSG_LEN,fmt,ap);
        va_end(ap);
	printf("DEBUG:[%s][%d] %s", __FILE__, __LINE__, stmp);
#endif
}
*/
int getCanWritePosition();
char *GetIniKeyString( char *Path, char *title, char *key , int *err);
int GetIniKeyInt( char *Path, char *title, char *key, int *err);

int main()
{
    int ret	 = 0;

    ret = getCanWritePosition();
    if(ret < 0)
    {
        printf("getCanWritePosition() err! ret = [%d]\n", ret);
    }
    else
    {
        slog("Position = [%d]\n", ret);
    }


    return 0;
}
int getCanWritePosition()
{
    int i = 0;
    int ret = 0;
    int intValue = 0;
    int intMin = 0;
    int intMinPosition = -1;
    char key[12];

    for(i = 0; i < 5; i++)
    {
        memset(key, 0x00, sizeof(key));
        sprintf(key, "wifiid%d", i);

        intValue = GetIniKeyInt(PATH, key, key, &ret);
        if( ret < 0 )
        {
            printf("GetIniKeyInt() err!\n");
            return -1;
        }
        else //GetIniKeyInt()正确返回
        {
            slog("intValue = [%d]\n", intValue);
            if( intValue == (-1) )
            {
                return i;
            }
            else
            {
                if(i == 0)
                {
                    intMin = intValue;
                }
                (intMin > intValue) ? (intMin = intValue, intMinPosition = i) : (1 == 1);
                slog("intMinPosition = [%d]\n", intMinPosition);
            }

        }
    }
    if(intMinPosition >= 0)
    {
        slog("intMin = [%d], intMinPosition = [%d]\n", intMin, intMinPosition);
        return intMinPosition;
    }
    slog("return -1!\n");
    return -1;
}
char *GetIniKeyString( char *Path, char *title, char *key , int *err)
{
    char		szLine[1024];
    static char	tmpstr[1024];
    int		rtnval;
    int		i	= 0;
    int		flag	= 0;
    char		*tmp;
    FILE		*fp = NULL;

    *err = -1;

    fp =  fopen( Path, "r" );
    if ( fp == NULL )
    {
        printf( "read open %s failed\n", Path );
        return( (char *) "-2");
    }
    while ( !feof( fp ) )
    {
        rtnval = fgetc( fp );
        if ( rtnval == EOF )
        {
            break;
        }
        else
        {
            szLine[i++] = rtnval;
        }
        if ( rtnval == '\n' )
        {
            if ( szLine[i - 1] == '\r' )
            {
                i--;
            }
            szLine[--i]	= '\0';
            i		= 0;
            tmp		= strchr( szLine, '=' );
            if ( (tmp != NULL) && (flag == 1) )
            {
                if ( strstr( szLine, key ) != NULL )
                {
                    if ( '#' == szLine[0] )
                    {
                    }
                    else if ( '/' == szLine[0] && '/' == szLine[1] )
                    {
                    }
                    else
                    {
                        strcpy( tmpstr, tmp + 1 );
                        fclose( fp );
                        *err = 0;
                        return(tmpstr);
                    }
                }
                else
                {
                    memset( szLine, 0, 1024 );
                }
            }
            else
            {
                strcpy( tmpstr, "[" );
                strcat( tmpstr, title );
                strcat( tmpstr, "]" );
                if ( strncmp( tmpstr, szLine, strlen( tmpstr ) ) == 0 )
                {
                    flag = 1;
                }
            }
        }
    }
    fclose( fp );
    return( (char *) "-2");
}
int GetIniKeyInt( char *Path, char *title, char *key, int *err)
{
    return(atoi( GetIniKeyString( Path, title, key, err ) ) );
}

